package com.dgut.staffmodule.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.staffmodule.dao.DepartmentRepository;
import com.dgut.staffmodule.dao.PositionRepository;
import com.dgut.staffmodule.dao.StaffRepository;
import com.dgut.staffmodule.entity.Department;
import com.dgut.staffmodule.entity.Position;
import com.dgut.staffmodule.entity.PositionEditDTO;
import com.dgut.staffmodule.entity.Staff;

@Service
@Transactional
public class PositionService implements IPositionService {
	@Autowired
	private PositionRepository positionRepository;// 职位dao
	@Autowired
	private DepartmentRepository departmentRepository;// 部门dao
	@Autowired
	private StaffRepository staffRepository;// 员工dao
	// 保存职位信息

	public void saveOrUpdate(Position position) {
		positionRepository.save(position);
	}

	// 删除职位
	public boolean delete(Position position) {
		position = positionRepository.findOne(position.getId());
		List<Staff> staffs = staffRepository.findByPos(position.getPositionName());
		// boolean flag = (position != null);
		// 该职位不存在员工才可删除
		// int flag1 = staffs.size();
		if ((position != null) && (staffs.size() == 0)) {
			position.setDepartment(null);
			positionRepository.delete(position);
			return true;
		} else {
			return false;
		}
	}

	// 批量删除职位
	public boolean delete(List<Long> ids) {
		List<Position> positions = (List<Position>) positionRepository.findAll(ids);
		List<Staff> staffs = new ArrayList<Staff>();
		for (Position p : positions) {
			staffs = staffRepository.findByPos(p.getPositionName());
		}
		if (positions != null && (staffs.size() == 0)) {
			for (Position p : positions) {
				positionRepository.delete(positions);
			}
			return true;
		} else {
			return false;
		}
	}

	// 查找单个职位
	@Transactional(readOnly = true)
	public Position findOne(Long id) {
		return positionRepository.findOne(id);
	}

	// 查找所有职位
	@Transactional(readOnly = true)
	public List<Position> findAll() {
		return (List<Position>) positionRepository.findAll();
	}

	// 分页查职位
	@Transactional(readOnly = true)
	public Page<Position> findAll(Specification<Position> spec, Pageable pageable) {
		return positionRepository.findAll(spec, pageable);
	}

	// 查找出该部门下的所有职位
	public List<Position> findAllByDeptId(Long id) {
		return positionRepository.findAllByDeptId(id);
	}

	// 保存前端的新增职位信息
	public void save(PositionEditDTO positionEditDTO) {
		Position position = new Position();
		Department department = departmentRepository.findByDeptName(positionEditDTO.getDepartment());
		position.setPositionName(positionEditDTO.getPositionName());
		position.setDepartment(department);
		positionRepository.save(position);
	}

	// 保存前端的更改的职位信息
	@Override
	public void update(PositionEditDTO positionEditDTO) {
		Position position = positionRepository.findOne(positionEditDTO.getId());
		Department department = departmentRepository.findByDeptName(positionEditDTO.getDepartment());
		List<Staff> staffs = staffRepository.findByPos(position.getPositionName(), department.getId());
		for (Staff staff : staffs) {
			staff.setPosition(positionEditDTO.getPositionName());
			System.out.println(staff);
		}

		position.setPositionName(positionEditDTO.getPositionName());
		position.setDepartment(department);
		positionRepository.save(position);
	}

	// 通过职称查找该职位
	@Override
	public Position findByPosName(String positionName) {
		Position position = positionRepository.findByPosName(positionName);
		return position;
	}

}
